/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2004
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 * 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.insaneSolver;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

import javax.swing.JOptionPane;

import br.ufmg.dees.insane.commons.view.ModelList;
import br.ufmg.dees.insane.model.femModel.FemModel;
import br.ufmg.dees.insane.model.postp.PostpModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.parser.FemModelToPostpModel;
import br.ufmg.dees.insane.persistence.PersistenceAsInsane;
import br.ufmg.dees.insane.persistence.StepModel;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;

/**
 * This Class loads the insane postprocessor
 * @author Penna, S. S., Pitangueira, Roque
 * @since 14/12/2006
 */
public class LoadPostp implements Runnable{
	
    public String savePath;
    public String saveName;
    public String solutionType;
    public boolean isPathSet;
    
	public LoadPostp(){
	}
	
	public void run() {
	    try{
	        DesktopPostp frame = new DesktopPostp();
	        frame.validate();
	        //Centers the window
	        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	        Dimension frameSize = frame.getSize();
	        if (frameSize.height > screenSize.height)
	            frameSize.height = screenSize.height;
	        if (frameSize.width > screenSize.width)
	            frameSize.width = screenSize.width;
	        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
	        frame.setVisible(true);
	        
	        if(isPathSet){
	            if(solutionType.equals("SteadyState")){
	                String aux = saveName.replace(".isn","");
	                Object objAux = new Object();
	                try{
	                    ObjectInputStream stream = new ObjectInputStream(new FileInputStream(PersistenceAsInsane.fileName));
	                    objAux = stream.readObject();
	                }catch (Exception exc) {
	                    Toolkit.getDefaultToolkit().beep();
	                    JOptionPane.showMessageDialog(frame, "Couldn't open file!", "Error Message", JOptionPane.ERROR_MESSAGE);
	                }
	                if(objAux instanceof StepModel){
	                    FemModel femModel = (FemModel)((StepModel)objAux).getModel();
	                    FemModelToPostpModel parserPostpModel = new FemModelToPostpModel(femModel);
	                    PostpModel postpModelMesh = new PostpModel();
	                    postpModelMesh.addStep(parserPostpModel.getStepPostp());
	                    postpModelMesh.setLabel(aux);
	                    ((ModelList)frame.getModelList()).removeModel(postpModelMesh.getLabel());
	                    ((ModelList)frame.getModelList()).putModel(postpModelMesh.getLabel(),postpModelMesh);
	                    
	                    InterfacePostp intFrame = new InterfacePostp();
	                    intFrame.validate();
	                    intFrame.setPostpModel(postpModelMesh);
	                    intFrame.setModel((StepPostp)postpModelMesh.getStepList().get(0));
	                    intFrame.setTitle(postpModelMesh.getLabel());
	                    frame.addInternalFrame(intFrame);
	                    intFrame.toFront();
                        frame.setInterface();
	                }else{
	                    JOptionPane.showMessageDialog(frame, "Couldn't open file!", "Error Message", JOptionPane.ERROR_MESSAGE);
	                }
	            }
	            
	            if(solutionType.equals("EquilibriumPath")){
                   
                    
                    
	            }
	        }
	        
	    }catch (Exception e){
	        System.out.println("Thread error Load Postp");
	    }
    }
	    
}//*************************** end class *************************
